package org.jeecg.modules.jxc.purchase.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.jxc.homepage.entity.JxcGoods;
import org.jeecg.modules.jxc.purchase.entity.GoodsType;
import org.jeecg.modules.jxc.purchase.entity.PurchaseGoods;
import org.jeecg.modules.jxc.purchase.entity.ReturnGoods;
import org.jeecg.modules.jxc.purchase.entity.ReturnPurchase;
import org.jeecg.modules.jxc.purchase.service.PurchaseService;
import org.jeecg.modules.jxc.statistical.entity.JxcPurchaseList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @description:
 * @company: yaorange
 * @author: Mr.Huang
 * @version: 1.0
 * @create: 2021-12-21 15:46:59
 */
@RestController
@RequestMapping("/purchase")
@Slf4j
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @GetMapping("/list")
    public Result<?> querySupplier(){
        List<org.jeecg.modules.demo.jxc.baseData.entity.JxcSupplier> list = purchaseService.getList();
        return Result.OK(list);
    }

    @GetMapping("/type")
    public Result<?> queryType(){

        List<GoodsType> list = purchaseService.getGoodsType();
        return Result.OK(list);

    }
    @GetMapping("/goods/{pageNo}/{pageSize}/{id}")
    public Result<?> queryGoods(@PathVariable Integer pageNo,@PathVariable Integer pageSize,@PathVariable Integer id){

        org.jeecg.modules.jxc.purchase.entity.Result<JxcGoods> list = purchaseService.getGoods(pageNo,pageSize,id);
        return Result.OK(list);

    }

    @GetMapping("/goodsCode/{id}")
    public Result<?> queryGoodsById(@PathVariable Integer id){

       JxcGoods goods = purchaseService.getGoodById(id);
       return Result.OK(goods);
    }

    @PostMapping("/addOrder/{orderId}")
    public Result<?> addOrder(@PathVariable String orderId, String purchaseList, JxcPurchaseList purchase){

        List<PurchaseGoods> list = JSON.parseArray(purchaseList, PurchaseGoods.class);
        purchaseService.addOrder(orderId,list,purchase);
        return Result.OK().success("请求成功");
    }

    @PostMapping("/returnOrder/{orderId}")
    public Result<?> returnOrder(@PathVariable String orderId, String purchaseList, ReturnPurchase purchase){

        List<ReturnGoods> list = JSON.parseArray(purchaseList, ReturnGoods.class);
        Boolean bl = purchaseService.returnOrder(orderId,list,purchase);
        if(bl){
            return Result.error("退货数量高于库存");
        }
        return Result.OK().success("请求成功");
    }

    @GetMapping("/purchaseListGoods/list")
    public Result<?> QueryPurchase(@RequestParam("purchaseNumber") String purchaseNumber,
                                   @RequestParam("supplierId") Integer supplierId,
                                   @RequestParam("state") Integer state,
                                   @RequestParam("sTime") String sTime,
                                   @RequestParam("eTime") String eTime){

        List<Map<String, Object>> list = purchaseService.findPurchaseList(purchaseNumber,supplierId,state,sTime,eTime);
        System.out.println(list);
        return Result.OK(list);

    }

}
